package com.cloudxingkong.dianyinke.Student.Adapter;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Class.MyGridView;
import com.cloudxingkong.dianyinke.Student.bean.KeMuTest;

import java.util.List;

/**
 * 科目ListViewAdapter
 * Created by ningfei on 2016/3/28.
 */
public class KeMuListViewAdapter extends BaseAdapter{
    private List<KeMuTest.ListBean> lists;
    private Context context;
    private FragmentManager manager;
    private static getGridviewPosition getGridviewPosition;

    public KeMuListViewAdapter(List<KeMuTest.ListBean> lists, Context context, FragmentManager manager, KeMuListViewAdapter.getGridviewPosition getGridviewPosition) {
        this.lists = lists;
        this.context = context;
        this.manager = manager;
        this.getGridviewPosition = getGridviewPosition;
    }

    @Override
    public int getCount() {
        return lists==null?0:lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            convertView=View.inflate(context,R.layout.kemu_lv_item,null);
            holder=new ViewHolder();
            holder.tvSection= (TextView) convertView.findViewById(R.id.lv_item_title);
            holder.gridView= (MyGridView) convertView.findViewById(R.id.listview_item_gridview);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        String pname = lists.get(position).getPname();
        Log.e("test","pname : "+pname);
        holder.tvSection.setText(pname);

        final List<KeMuTest.ListBean.ChildrenBean> children = lists.get(position).getChildren();

        KeMuGridViewAdapter gridViewAdapter = new KeMuGridViewAdapter(children, context);

        holder.gridView.setAdapter(gridViewAdapter);
        gridViewAdapter.notifyDataSetChanged();

        holder.gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));

        holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            /**
             * 实现最底层接口
             */
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String name = children.get(position).getName();
                Log.e("test","科目:"+name);
                com.cloudxingkong.dianyinke.Student.Adapter.KeMuListViewAdapter.getGridviewPosition.getPosition(position,name);
            }
        });

        return convertView;
    }
    private class ViewHolder{
        TextView tvSection;
        MyGridView gridView;
    }
    //接口
    public interface getGridviewPosition{
        public void getPosition(int position,String pname);
    }

}
